package com.cjm.one.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjm.one.common.exception.GlobalException;
import com.cjm.one.common.result.ResultCodeEnum;
import com.cjm.one.model.base.BaseDeleteMoreVo;
import com.cjm.one.model.system.dto.SysPostDto;
import com.cjm.one.model.system.entity.SysUserPost;
import com.cjm.one.system.mapper.SysPostMapper;
import com.cjm.one.system.mapper.SysUserPostMapper;
import com.cjm.one.system.service.SysPostService;
import com.cjm.one.model.system.entity.SysPost;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 29289
* @description 针对表【sys_post(岗位表)】的数据库操作Service实现
* @createDate 2024-09-05 16:21:14
*/
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost>
    implements SysPostService {
    @Resource
    private SysPostMapper sysPostMapper;
    @Resource
    private SysUserPostMapper sysUserPostMapper;

    @Override
    public Page<SysPost> selectList(Page<SysPost> sysPostPage, SysPostDto dto) {
        LambdaQueryWrapper<SysPost> wrapper = new LambdaQueryWrapper<>();
        if (dto.getDeptId()!=null){
            wrapper.eq(SysPost::getDeptId, dto.getDeptId());
        }else{
            wrapper.eq(SysPost::getDeptId, 0);
        }

        if (dto.getPostName() != null) {
            wrapper.like(SysPost::getPostName, dto.getPostName());
        }
        if (dto.getPostCode() != null) {
            wrapper.eq(SysPost::getPostCode, dto.getPostCode());
        }
        if (dto.getPostStatus() != null) {
            wrapper.eq(SysPost::getPostStatus, dto.getPostStatus());
        }
        wrapper.orderByDesc(SysPost::getPostSort);
        return sysPostMapper.selectPage(sysPostPage, wrapper);

    }

    @Override
    public Boolean deleteById(Long id) {
        LambdaQueryWrapper<SysUserPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserPost::getPostId, id);
        SysUserPost sysUserPost = sysUserPostMapper.selectOne(wrapper);
        if (sysUserPost != null) {
           throw new GlobalException(ResultCodeEnum.DELETE_POST_ERROR);
        }
        return sysPostMapper.deleteById(id) > 0;
    }

    @Override
    public BaseDeleteMoreVo deleteBatchIds(List<Long> ids) {
        LambdaQueryWrapper<SysUserPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysUserPost::getPostId, ids);
        sysUserPostMapper.delete(wrapper);
        int i = sysPostMapper.deleteBatchIds(ids);
        BaseDeleteMoreVo baseDeleteMoreVo = new BaseDeleteMoreVo();
        baseDeleteMoreVo.setFlag(false);
        baseDeleteMoreVo.setTotal(ids.size());
        baseDeleteMoreVo.setCount(i);
        if (i==ids.size()) {
            baseDeleteMoreVo.setFlag(true);
            return baseDeleteMoreVo;
        }
        return baseDeleteMoreVo;
    }
}




